import Kodo from './client/kodo';
import OSS from './client/oss';
import SRT from './client/srt';
import Store from '@/store';
import Storage from '@/lib/storage';

function fetchUploadConfig() {
    return G_transmit('getMaterialUploadToken', {
            data: {
                slice: 1
            }
        })
        .then((res) => res.data);
}

export default async function createUploader() {
    const config = await fetchUploadConfig();
    const pType = config.ispType;

    if (pType === 'qiNiu') {
        return new Kodo(config.qiniuTokenResponse);
    } else if (pType === 'oss') {
        return new OSS({
            accessKeyId: config.appTokenResponse.accessKeyId,
            accessKeySecret: config.appTokenResponse.accessKeySecret,
            stsToken: config.appTokenResponse.securityToken,
            bucket: config.appTokenResponse.bucketName,
            endpoint: config.appTokenResponse.endpoint,
            region: config.appTokenResponse.ossRegion,
            host: config.appTokenResponse.host,
            path: config.appTokenResponse.path,
            callBackBody: config.appTokenResponse.callBackBody
        });
    } else if (pType === 'srt') {
        const Cookies = new Storage('cookie');

        return new SRT({
            accessToken: Cookies.getItem('X-DT-accessToken') || '',
            clientId: 'sanren-teacher-pc',
            token: config.srtTokenResponse.token,
            host: config.srtTokenResponse.host,
            uploadHost: config.srtTokenResponse.uploadHost
        });
    }
}
